package com.spa.infrastructure.repository.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.spa.infrastructure.enums.dict.order.OrderTransferStatusEnum;
import com.spa.infrastructure.repository.condition.OrderTransferCondition;
import com.spa.infrastructure.repository.mapper.OrderTransferMapper;
import com.spa.infrastructure.repository.po.OrderTransferPO;
import com.spa.infrastructure.util.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * domain service
 * 订单转单表(OrderTransfer) 
 *
 * @author makejava
 * @since 2024-01-31 11:38:17
 */
@Component
public class OrderTransferRepositoryService extends MPJBaseServiceImpl<OrderTransferMapper, OrderTransferPO> {
	

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public OrderTransferPO queryById(Long id) {
		return getById(id);
    }
	
	
	/**
     * 分页查询
     * @return 查询结果
     */
	public Page<OrderTransferPO> pageListInfo(OrderTransferCondition condition) {
		LambdaQueryWrapper<OrderTransferPO> wrapper = wrapper(condition);
        return page(Page.of(condition.getCurrent(), condition.getSize()), wrapper);
    }
	
	public List<OrderTransferPO> listInfo(OrderTransferCondition condition) {
        return list(wrapper(condition));

    }

    /**
     * 按订单id分组，查询出每个订单最新的一条转单记录
     * @param condition
     * @return
     */
    public List<OrderTransferPO> list4GroupByOrderLimitOne(OrderTransferCondition condition) {
        return list(MPJwrapper(condition));

    }

    /**
     * 按订单id分组，查询出每个订单最新的一条转单记录
     * @param condition
     * @return
     */
    public Page<OrderTransferPO> page4GroupByOrderLimitOne(OrderTransferCondition condition) {
        return page(Page.of(condition.getCurrent(), condition.getSize()), MPJwrapper(condition));

    }

    private MPJLambdaWrapper<OrderTransferPO> MPJwrapper(OrderTransferCondition condition) {
        MPJLambdaWrapper<OrderTransferPO> wrapper = JoinWrappers.lambda(OrderTransferPO.class);
        wrapper.selectAll(OrderTransferPO.class)
                .innerJoin("( SELECT MAX( id ) AS id FROM s_order_transfer GROUP BY order_id  ) t2 ON t.id = t2.id");
        wrapper.eq(ObjectUtil.isNotEmpty(condition.getShopId()), OrderTransferPO::getShopId, condition.getShopId());
        wrapper.eq(ObjectUtil.isNotEmpty(condition.getOrderId()), OrderTransferPO::getOrderId, condition.getOrderId());
        wrapper.eq(ObjectUtil.isNotEmpty(condition.getHandleMark()), OrderTransferPO::getHandleMark, condition.getHandleMark());
        wrapper.eq(ObjectUtil.isNotEmpty(condition.getStatus()), OrderTransferPO::getStatus, condition.getStatus());
        wrapper.eq(StringUtils.isNotEmpty(condition.getOrderCode()), OrderTransferPO::getOrderCode, condition.getOrderCode());
        wrapper.like(StringUtils.isNotEmpty(condition.getOperName()), OrderTransferPO::getOperName, condition.getOperName());
        wrapper.orderByDesc(StringUtils.isNotEmpty(condition.getDescCreateTime()), OrderTransferPO::getCreateTime);
        if (ObjectUtil.isEmpty(condition.getAll())) {
            wrapper.eq(OrderTransferPO::getIsDel, 0);
        }
        return wrapper;
    }

	private LambdaQueryWrapper<OrderTransferPO> wrapper(OrderTransferCondition condition) {
        LambdaQueryWrapper<OrderTransferPO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotEmpty(condition.getOrderId()), OrderTransferPO::getOrderId, condition.getOrderId());
        wrapper.in(CollectionUtil.isNotEmpty(condition.getOrderIds()), OrderTransferPO::getOrderId, condition.getOrderIds());
        wrapper.eq(ObjectUtil.isNotEmpty(condition.getShopId()), OrderTransferPO::getShopId, condition.getShopId());
        wrapper.eq(ObjectUtil.isNotEmpty(condition.getStatus()), OrderTransferPO::getStatus, condition.getStatus());
        wrapper.eq(ObjectUtil.isNotEmpty(condition.getOrderCode()), OrderTransferPO::getOrderCode, condition.getOrderCode());
        wrapper.eq(ObjectUtil.isNotEmpty(condition.getOperName()), OrderTransferPO::getOperName, condition.getOperName());
        wrapper.orderByAsc(ObjectUtil.equals(condition.getStatus(), OrderTransferStatusEnum.$10.getCode()), OrderTransferPO::getOrderCreateTime);
        wrapper.orderByAsc(StringUtils.isNotEmpty(condition.getAscCreateTime()), OrderTransferPO::getCreateTime);
        wrapper.orderByDesc(ObjectUtil.equals(condition.getStatus(), OrderTransferStatusEnum.$20.getCode()), OrderTransferPO::getOrderCreateTime);
        wrapper.orderByDesc(StringUtils.isNotEmpty(condition.getDescCreateTime()), OrderTransferPO::getCreateTime);
        if (ObjectUtil.isEmpty(condition.getAll())) {
            wrapper.eq(OrderTransferPO::getIsDel, 0);
        }
        return wrapper;
    }

    public boolean add(OrderTransferPO orderTransferPO, Long createId, String createName) {
        orderTransferPO.setCreateBy(createId.toString());
        orderTransferPO.setCreateName(createName);
        orderTransferPO.setCreateTime(new Date());
        orderTransferPO.setIsDel(0);
        return save(orderTransferPO);
    }

    public boolean exists(OrderTransferCondition condition) {
        return exists(wrapper(condition));
    }

    public Long count(OrderTransferCondition condition) {
        return count(wrapper(condition));
    }

    public OrderTransferPO getOne(OrderTransferCondition condition) {
        return getOne(wrapper(condition));
    }

    public boolean update(OrderTransferPO orderTransferPO,OrderTransferCondition condition) {
        return update(orderTransferPO, wrapper(condition));
    }
}
